import {ActionName, type ActionType} from "@/components/type";

export default class Action {
  private sequence_: ActionType[] = [];
  private plantTimes_: number = 0;
  private waterTimes_: number = 0;
  private farmTimes_: number = 0;
  private flowerTimes_: number = 0;
  private fruitTimes_: number = 0;
  private beehiveTimes_: number = 0;
  private toolTimes_: number = 0;
  static bonusAction: ActionName[] = [
    ActionName.FLOWER,
    ActionName.BEEHIVE,
    ActionName.FRUIT,
    ActionName.TOOL
  ];
  static farmAction: ActionName[] = [
    ActionName.PLANT,
    ActionName.WATER
  ];

  // 新增行动序列
  private pushAction(action: ActionType) {
    this.sequence_.push(action);
  }

  // 扣除行动次数
  useAction(action: ActionType) {this.actionsOperation(action, false);}

  // 设置行动次数
  setAction(actions: ActionType[]) {
    actions.forEach(action => {
      this.pushAction(action);
      this.actionsOperation(action);
    });
  }

  // 根据不同情况操作行动次数
  private actionsOperation(name: ActionType, operations: boolean = true) {
    // operations false 则增加行动； operations true 则减少行动
    switch (name) {
      case ActionName.PLANT:
        operations ? this.plantTimes_ += 2 : this.plantTimes_--;
        console.log(`种植次数剩余${this.plantTimes}次`);
        break;
      case ActionName.WATER:
        operations ? this.waterTimes_++ : this.waterTimes_--;
        console.log(`灌溉次数剩余${this.waterTimes}次`);
        break;
      case ActionName.FARM:
        operations ? this.farmTimes_++ : this.farmTimes_--;
        console.log(`农垦行动剩余${this.farmTimes}次`);
        break;
      case ActionName.FLOWER:
        operations ? this.flowerTimes_++ : this.flowerTimes_--;
        console.log(`花卉次数剩余${this.flowerTimes}次`);
        break;
      case ActionName.BEEHIVE:
        operations ? this.beehiveTimes_++ : this.beehiveTimes_--;
        console.log(`蜂巢次数剩余${this.beehiveTimes}次`);
        break;
      case ActionName.FRUIT:
        operations ? this.fruitTimes_++ : this.fruitTimes_--;
        console.log(`果园次数剩余${this.fruitTimes}次`);
        break;
      case ActionName.TOOL:
        operations ? this.toolTimes_++ : this.toolTimes_--;
        console.log(`工具次数剩余${this.toolTimes}次`);
        break;
      case ActionName.BONUS:
        operations ? this.waterTimes_++ : this.waterTimes_--;
        console.log(`奖励行动剩余${this.waterTimes}次`);
        break;
      case ActionName.RAIN:
        this.waterTimes_ = 6;
        console.log(`下雨，获得灌溉行动${this.waterTimes}次`);
        break;
    }
  }

  get sequence() {
    return this.sequence_;
  }

  // 回合结束
  get overTurn() {
    return this.sequence_.length === 0;
  }

  get isCanFarm(): boolean {return this.plantTimes === 0 && this.waterTimes === 0;}

  get farmTimes() {return this.farmTimes_;}

  get plantTimes(): number {return this.plantTimes_;}

  get waterTimes(): number {return this.waterTimes_;}

  get flowerTimes(): number {return this.flowerTimes_;}

  get fruitTimes(): number {return this.fruitTimes_;}

  get beehiveTimes(): number {return this.beehiveTimes_;}

  get toolTimes(): number {return this.toolTimes_;}

  // 单例模式
  private static instance: Action | null = null;

  public static getInstance(): Action {
    if (this.instance === null) {
      this.instance = new Action();
    }
    return this.instance;
  }
}


